import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/index.js'
const res_code = 200

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		code: "",
		access_token: "",
		openid: "", // obD_t1DQGbgVrCFnluGvhS12uhR8 obD_t1CJQMV3LhLx0psum-iUdudI
		appid: "",
		secret: "",
		masterColor: "#335FFC",
		isLogin: true,
		tabbar: [],
		partyList: [],
		partyItem: '',
		screenHeight:0,
		contentHeight:0,
		navbarHeight:0,
		list_show_type_str: '',
		childCategoryList:[],
		categoryId:'',
		title:'测试',
		member:'',
		userInfo:'',
		weixinConfig:'',
		isWeixin:false,
		keywordsList:''
	},
	mutations: {
		setIsWeixin(state, rawData) {
			state.isWeixin = rawData;
		},
		setWeixinConfig(state, rawData) {
			state.weixinConfig = rawData;
		},
		setTitle(state, rawData) {
			state.title = rawData;
			document.title = rawData
		},
		setPartyList(state, rawData) {
			state.partyList = rawData;
		},
		setCategoryId(state, rawData) {
			state.categoryId = rawData;
			console.log("result[0].id",state.categoryId)
		},
		setChildCategoryList(state, rawData) {
			state.childCategoryList = rawData;
		},
		setListShowTypeStr(state, rawData) {
			state.list_show_type_str = rawData;
			localStorage.setItem('list_show_type_str',rawData)
		},
		setPartyItem(state, rawData) {
			state.partyItem = rawData;
			console.log("state.informationItem",state.partyItem)
		},
		setScreenHeight(state, rawData) {
			state.screenHeight = rawData;
			localStorage.setItem('screenHeight',rawData)
		},
		setContentHeight(state, rawData) {
			state.contentHeight = rawData;
			localStorage.setItem('contentHeight',rawData)
		},
		setAccessToken(state, rawData) {
			state.access_token = rawData;
		},
		setOpenid(state, rawData) {
			state.openid = rawData;
			localStorage.setItem('openid',rawData)
		},
		setCode(state, rawData) {
			state.code = rawData;
			localStorage.setItem('code',rawData)
		},
		setAppid(state, rawData) {
			state.appid = rawData;
		},
		setSecret(state, rawData) {
			state.secret = rawData;
		},
		setUserInfo(state, rawData) {
		  state.userInfo = rawData;
		},
		setTabbar(state, rawData) {
			state.tabbar = rawData;
		},
		setMasterColor(state, rawData) {
			state.masterColor = rawData;
			console.log(state.masterColor)
		},
		setMember(state, rawData) {
			state.member = rawData;
			console.log('setMember',rawData)
			localStorage.setItem('member',JSON.stringify(rawData))
			console.log('setedMember',JSON.parse(localStorage.getItem('member')))
		},
		setKeywordsList(state, rawData) {
			state.keywordsList = rawData;
		},
	},
	actions: {
		Match_policy ({ commit }, params) {
			return new Promise((resolve, reject) => {
				api.matePolicy(params).then(res => {
					resolve(res)
				}).catch(error => {
				  reject(error)
				})
			})
		},
		GET_Keywords ({ commit }, params) {
			return new Promise((resolve, reject) => {
				api.getKeywords(params).then(res => {
					if(res.code == res_code){
						// const result = res.data.list
						commit('setKeywordsList', res.data.list)
						resolve(res)
					}
				}).catch(error => {
				  reject(error)
				})
			})
		},
		GET_AllMember ({ commit }, params) {
			return new Promise((resolve, reject) => {
				api.getAllMember(params).then(res => {
					if(res.code == res_code){
						const result = res.data
						commit('setMember', result)
						resolve(res)
					}
				}).catch(error => {
				  reject(error)
				})
			})
		},
		GET_Member ({ commit }, params) {
			return new Promise((resolve, reject) => {
				api.getMember(params).then(res => {
					if(res.code == res_code){
						const result = res.data
						commit('setMember', result)
						resolve(result)
					}
				}).catch(error => {
				  reject(error)
				})
			})
		},
		// 获取子列表 getChildCategory
		GET_ChildCategory ({ commit }, params) {
			return new Promise((resolve, reject) => {
				api.getChildCategory(params).then(res => {
					if(res.code == res_code){
						const result = res.data
						commit('setChildCategoryList', result)
						console.log('result[0].id',result[0].id)
						commit('setCategoryId', result[0].id)
						resolve(result)
					}
				}).catch(error => {
				  reject(error)
				})
			})
		},
		// 获取资讯详情 information_detil
		GET_PartyItem ({ commit }, params) {
			return new Promise((resolve, reject) => {
				api.newsDetails(params).then(res => {
					if(res.code == 200){
						const result = res.data
						commit('setPartyItem', result)
						resolve(result)
					}
				}).catch(error => {
				  reject(error)
				})
			})
		},
		
		SET_GOODS_LIST ({ commit }, data) {
		  commit('SET_GOODS_LIST',data);
		},
	},
	getters: {
		keywordsList:state=>state.keywordsList,
		isWeixin:state=>state.isWeixin,
		weixinConfig:state=>state.weixinConfig,
		member(state) {
			return state.member || JSON.parse(localStorage.getItem('member'));
		},
		code:state=>state.code,
		title:state=>state.title,
		partyList:state=>state.partyList,
		categoryId:state=>state.categoryId,
		childCategoryList:state=>state.childCategoryList,
		list_show_type_str:state=>state.list_show_type_str,
		navbarHeight:state=>state.navbarHeight,
		img_urls:state=>state.img_urls,
		partyItem:state=>state.partyItem,
		contentHeight:state=>state.contentHeight,
		screenHeight:state=>state.screenHeight,
		openid(state) {
			return state.openid || localStorage.getItem('openid');
		},
		getAppid(state) {
			return state.appid;
		},
		getSecret(state) {
			return state.secret;
		},
		getMasterColor(state) {
			return state.masterColor;
		},
		getTabbar(state) {
			return state.tabbar;
		},
	},
	modules: {}
})
